/*
 * Digital Audio Mobile - A multi track studio for Windows Mobile
 * Copyright (C) 2008 David Olsson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#if !defined(__CLICKGENERATOR_H__)
#define __CLICKGENERATOR_H__

class AudioBuffer;
class MusicTime;

class ClickGenerator {
public:
    ClickGenerator() throw();

    void reset(const MusicTime &offset, unsigned int bpm, unsigned int sigBpm, unsigned int sigBnt) throw();

    void generateClicks(AudioBuffer *buffer) throw();
private:
    typedef void *(*TimeToPtr)(void *basePtr, float time);
    typedef float (*PtrToTime)(void *ptr);
    typedef void *(*PtrToNextSample)(void *ptr);
    typedef void (*WriteSample)(void *ptr, float value);

    static void writeClickPulse(void *start, const void *endOfBuffer, TimeToPtr timeToPtr, PtrToNextSample ptrToNextSample, WriteSample writeSample, bool acc = false) throw();

    // 11.025kHz, mono, 8-bits samples.
    static void *timeToPtr1M8(void *basePtr, float time) throw();
    static float ptrToTime1M8(void *ptr) throw();
    static void *ptrToNextSample1M8(void *ptr) throw();
    static void writeSample1M8(void *ptr, float value) throw();
    // 11.025kHz, mono, 16-bits samples.
    static void *timeToPtr1M16(void *basePtr, float time) throw();
    static float ptrToTime1M16(void *ptr) throw();
    static void *ptrToNextSample1M16(void *ptr) throw();
    static void writeSample1M16(void *ptr, float value) throw();

    // 22.050kHz, mono, 8-bits samples.
    static void *timeToPtr2M8(void *basePtr, float time) throw();
    static float ptrToTime2M8(void *ptr) throw();
    static void *ptrToNextSample2M8(void *ptr) throw();
    static void writeSample2M8(void *ptr, float value) throw();
    // 22.050kHz, mono, 16-bits samples.
    static void *timeToPtr2M16(void *basePtr, float time) throw();
    static float ptrToTime2M16(void *ptr) throw();
    static void *ptrToNextSample2M16(void *ptr) throw();
    static void writeSample2M16(void *ptr, float value) throw();

    // 44.100kHz, mono, 8-bits samples.
    static void *timeToPtr4M8(void *basePtr, float time) throw();
    static float ptrToTime4M8(void *ptr) throw();
    static void *ptrToNextSample4M8(void *ptr) throw();
    static void writeSample4M8(void *ptr, float value) throw();
    // 44.100kHz, mono, 16-bits samples.
    static void *timeToPtr4M16(void *basePtr, float time) throw();
    static float ptrToTime4M16(void *ptr) throw();
    static void *ptrToNextSample4M16(void *ptr) throw();
    static void writeSample4M16(void *ptr, float value) throw();

    unsigned int m_bpm;
    unsigned int m_sigBpm, m_sigBnt;

    unsigned int m_beat;
    float m_offset;

    static const float ACCCLICKFREQ;
    static const float CLICKFREQ;
    static const float CLICKDURARTION;
    static const float CLICKAMPLITUDE;
};

#endif
